#include "Stats.h"


static const int row = 3;


void drawGameStats(GameStats& stats, const iArr& iD, const fArr& fD)
{
    // Size of text buffers for the game stats
    static const Uint8 s_size = 5;
    // Arrays for character stats
    static char p1_buff[s_size], p2_buff[s_size], tie_buff[s_size];
    // Temporary surfaces for drawing and updating the scores
    static SDL_Surface *p1  = nullptr;
    static SDL_Surface *p2  = nullptr;
    static SDL_Surface *tie = nullptr;

    snprintf(p1_buff, sizeof(p1_buff), "%u", stats.p1Score());
    p1 = RenderTTF(fD[1], p1_buff, {0xFF, 0xFF, 0xFF}, TextMode::SOLID);
    PlaceSurface(iD[1] -> w + 95, iD[1] -> h - 330, p1, iD[0], 0);

    snprintf(p2_buff, sizeof(p2_buff), "%u", stats.p2Score());
    p2 = RenderTTF(fD[1], p2_buff, {0xFF, 0xFF, 0xFF}, TextMode::SOLID);
    PlaceSurface(iD[1] -> w + 95, iD[1] -> h - 280, p2, iD[0], 0);

    snprintf(tie_buff, sizeof(tie_buff), "%u", stats.Tie());
    tie = RenderTTF(fD[1], tie_buff, {0xFF, 0xFF, 0xFF}, TextMode::SOLID);
    PlaceSurface(iD[1] -> w + 95, iD[1] -> h - 225, tie, iD[0], 0);

    SDL_FreeSurface(p1);
    SDL_FreeSurface(p2);
    SDL_FreeSurface(tie);
}


void updateGameStats(SDL_Event ev, Mark (&game_board)[row][row], iArr& iD, const fArr& fD, GameStats& stats)
{
    if (ev.type == SDL_MOUSEBUTTONDOWN)
    {
        if (ev.button.button == SDL_BUTTON_LEFT)
        {
            if (checkVictory(game_board, Mark::X_MARK))
            {
                stats.incP1Score();
                Restart(iD, game_board);
            }

            else if (checkVictory(game_board, Mark::O_MARK))
            {
                stats.incP2Score();
                Restart(iD, game_board);
            }

            else if (checkTie(game_board))
            {
                stats.incTie();
                Restart(iD, game_board);
            }
        }
    }
}
